/**
 * 此类为入口和全局初始化 只需调用 init()即可
 * 2020-8-29 ICPC 整理
 * */
//lib
const data_control = require('./lib/data_control');
const physics_tool = require('./lib/physics_tool');
const node_component = require('./lib/node_component');

//service
const object_name = 'ICPC';
class icpc {
    static doInit = false;
    constructor() {
        //
        if (!icpc.doInit) {
            throw '初始化请调用 静态方法 init()';
        }
        //静态库
        this.lib = {data_control, physics_tool, node_component};
        //动态服务
        this.service = {};
    }

    emit(event, ...args) {
        this.Canvas().emit(event, ...args);
    }

    on(event, ...args) {
        this.Canvas().on(event, ...args);
    }

    Canvas() {
        return cc.find('Canvas');
    }

    //初始化
    static init() {
        this.doInit = true;
        //NodeJs判断 导入到cocos creator后可以删除此判断
        try {
            let w = window;
        } catch (e) {
            global['window'] = global;
        }
        //
        if (window[object_name] instanceof this) {
            return;
        } else {
            window[object_name] = new this();
        }
    }
}

module.exports = icpc;